package com.hyts.dbmeta.handler.mysql;

import com.hyts.dbmeta.base.AbstractConnectionConfig;
import com.hyts.dbmeta.config.DbDriverClassConfig;
import com.hyts.dbmeta.base.AbstractConnectionHandler;

import java.sql.Connection;
import java.util.logging.Logger;

/**
 * project-name:coffice-wizard
 * package-name:com.hyts.dbmeta.handler.mysql
 * author:Libo/Alex
 * create-date:2018-11-04 18:32
 * copyright:libo-hyts-github
 * email:libo2dev@aliyun.com
 * description:此类主要用于: mysql数据库的操作服务
 */
public class MySqlConnectionHandler extends AbstractConnectionHandler {



    private Logger logger = Logger.getLogger(MySqlConnectionHandler.class.getName());


    /**
     * @param connectionConfig
     */
    public MySqlConnectionHandler(AbstractConnectionConfig connectionConfig) {
        super(connectionConfig);
    }

    /**
     * 注入相关的数据库-driver驱动类
     * @return
     */
    @Override
    protected boolean injectClassLoader() {
        try {
            Class.forName(DbDriverClassConfig.MYSQL_V5_V6_DRIVER_CLASS);
        } catch (ClassNotFoundException e) {
            logger.severe("初始化数据库驱动失败!"+e.getMessage());
            return false;
        }
        return true;
    }

    /**
     * @param ipAddress
     * @param port
     * @param userName
     * @param password
     * @param database
     * @return
     */
    public Connection getMysqlConnection(String ipAddress,Integer port,String userName,String password,String database){
        injectClassLoader();
        String mysqlConnectionStr = connectionConfig.getConnectionStr(ipAddress,port,database);
        mysqlConnectionStr = mysqlConnectionStr.substring(0,mysqlConnectionStr.length()-1);
        return this.getConnection(userName,password,mysqlConnectionStr);
    }
    /**
     * @param port
     * @param userName
     * @param password
     * @param database
     * @param encoding
     * @return
     */
    public Connection getMysqlConnection(Integer port,String userName,String password,String database,String encoding){
        injectClassLoader();
        String mysqlConnectionStr = connectionConfig.getConnectionStr(port,database,encoding);
        return this.getConnection(userName,password,mysqlConnectionStr);
    }

    /**
     * @param ipAddress
     * @param database
     * @param userName
     * @param password
     * @return
     */
    public Connection getMysqlConnection(String ipAddress,String database,String userName,String password){
        injectClassLoader();
        String mysqlConnectionStr = connectionConfig.getConnectionStr(ipAddress,3306,database,userName,password);
        return this.getConnection(mysqlConnectionStr);
    }

    /**
     * @param ipAddress
     * @param port
     * @param database
     * @return
     */
    public Connection getMysqlConnection(String ipAddress,int port,String database,String encoding){
        injectClassLoader();
        String mysqlConnectionStr = connectionConfig.getConnectionStr(ipAddress,port,database,encoding);
        return this.getConnection(mysqlConnectionStr);
    }

    /**
     * @param ipAddress
     * @param port
     * @param database
     * @return
     */
    public Connection getMysqlConnection(String ipAddress,int port,String database){
        injectClassLoader();
        String mysqlConnectionStr = connectionConfig.getConnectionStr(ipAddress,port,database);
        return this.getConnection(mysqlConnectionStr);
    }

}
